<template>
  <div>
    <ul ref="ul">
      <li
        :data-num="item.num"
        :class="item.num === $store.state.chat.groupNum ? 'active' : ''"
        v-for="item in list"
        :key="item.num"
        @click="liClick_fn(item.num)"
      >
        <el-badge :value="item.badge" :hidden="item.badge == 0">
          <avatar
            :width="40"
            :height="40"
            :src="item.avatarUrl"
            class="avatar"
          />
        </el-badge>
        <div class="right">
          <div class="title">
            {{ item.name }}
          </div>
          <div class="msg" v-if="item.messages.length">
            <span v-if="item.messages[item.messages.length - 1].isWithDraw">
              <span v-if="item.messages[item.messages.length - 1].uid !== uid"
                >{{
                  item.messages[item.messages.length - 1].nick
                }}撤回了一条消息</span
              ><span v-else>你撤回了一条消息</span>
            </span>
            <span
              v-else
              v-show="item.messages[item.messages.length - 1].uid !== uid"
              >{{ item.messages[item.messages.length - 1].nick + "：" }}</span
            >{{ item.messages[item.messages.length - 1].con }}
          </div>
        </div>
      </li>
    </ul>
    <div ref="menu" class="right-menu"></div>
  </div>
</template>

<script>
import Avatar from "../../../components/coms/Avatar.vue";
import utils_m from "../../../utils";
import list_m from "../../../util/groupData";
export default {
  data() {
    return {
      activeLi: -1,
      uid: localStorage.getItem("uid"),
      rightMenuList: utils_m.rightMenuList,
      list: list_m.groupList,
    };
  },
  mounted() {
    console.log(this.list);
    let liDom;
    function getLi_fn(el) {
      let li = null;
      if (el.tagName === "LI") {
        liDom = el;
        return;
      } else {
        getLi_fn(el.parentNode);
      }
    }
    this.$refs.ul.oncontextmenu = (e) => {
      getLi_fn(e.target);
      utils_m.showList(1);
      let num = liDom.dataset.num - 0;
      this.$store.state.rightMenuX = e.x;
      this.$store.state.rightMenuY = e.y;
      this.$store.commit("updateRightMenuActiveGroupNum_fn", num);
      this.$store.commit("updateRightMenuShow_fn", true);
      return false;
    };
  },
  methods: {
    liClick_fn(num) {
      this.$store.commit("chat/setGroupNum_fn", num);
      let item = list_m.groupList.find((item) => item.num === num);
      if (item) {
        item.badge = 0;
      }
    },
    // 右键菜单点击项回调函数
    rightMenuClick_fn: utils_m.menuClick_fn,
  },
  components: {
    Avatar,
  },
};
</script>
<style lang="less" scoped>
li {
  height: 64px;
  display: flex;
  align-items: center;
  padding-left: 12px;
  cursor: default;
  &:hover {
    background-color: #d2d2d2;
  }
  &.active {
    background-color: #c5c5c5;
  }
  .avatar {
    // width: 40px;
    // height: 40px;
    // border: 1px solid #ccc;
  }
  /deep/.el-badge {
    .el-badge__content {
      border: none;
    }
  }
  .right {
    height: 40px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding-left: 12px;
    .title {
      font-size: 14px;
    }
    .msg {
      width: 140px;
      font-size: 12px;
      color: #999;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
    }
  }
}
.right-menu {
  position: fixed;
  left: 0;
  top: 0;
}
</style>